﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class KhoDAO:DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            KhoDTO objectDTO = new KhoDTO();
            objectDTO.Gui = int.Parse(dt.Rows[i]["Gui"].ToString());
            objectDTO.MaChiNhanh = int.Parse(dt.Rows[i]["MaChiNhanh"].ToString());
            objectDTO.MaKho = int.Parse(dt.Rows[i]["MaKho"].ToString());
            objectDTO.MaMonHang = int.Parse(dt.Rows[i]["MaMonHang"].ToString());
            objectDTO.TenKho = dt.Rows[i]["TenKho"].ToString();
            return (object)objectDTO;
        }

        public KhoDTO[] GetAll()
        {
            String strSQL = "Select KHO.MaKho, Kho.TenKho, KHO.MaChiNhanh, KHO_CT.MaMonHang, KHO_CT.Gui from KHO,KHO_CT Where KHO.MaKho = KHO_CT.MaKho ";
            DataTable dt = this.ExecuteQuery(strSQL);

            KhoDTO[] khoDTOs = new KhoDTO[dt.Rows.Count] ;
            for(int i=0; i<dt.Rows.Count; i++)
                khoDTOs[i] = (KhoDTO)GetDataFromDataRow(dt, i);
            
            return khoDTOs;
        }

        public void insert(KhoDTO info)
        {
            string insertCommand = "INSERT KHO VALUES(" +
                info.MaKho + ", '" +
                info.TenKho + "', " +
                info.MaChiNhanh + ")";

            ExecuteNonQuery(insertCommand);

           
        }

        public void update(KhoDTO info)
        {
            string updateCommand = "UPDATE KHO " +
                                    "SET TenKho = '" + info.TenKho + "', " +
                                    " MaChiNhanh = " + info.MaChiNhanh +
                                    " WHERE MaKho = " + info.MaKho;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(string maKho)
        {
            string deleteCommand = "DELETE FROM KHO WHERE MaKho = " + maKho;
            ExecuteNonQuery(deleteCommand);
        }

    }
}
